package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.ServerMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.entity.servercenter.ServerInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class ExpireDataDelete extends BaseRepair {
    public ExpireDataDelete(GameServer gameServer) {
        super(gameServer);
    }


    @Override
    public void repair(long param1, String param2) {

    }

    public void go() {
        ServerInfo serverInfo = getGameServer().getServerInfo();

        List<Long> allUserId = getAllUserId();
        Collections.sort(allUserId);
        for (Long userId : allUserId) {
            deleteBeautySkillRefineLog(userId);
            deleteBeautyGlamourLog(userId);
            deletePatronsUpgradeLog(userId);
            deleteClinicLog(userId);
            deleteManorHarvestLog(userId);
        }

    }

    private List<Long> getAllUserId() {
        String sql = "select userId from t_u_user";
        List<Long> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i = 1;
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(rs.getLong("userId"));
            }
        } catch (SQLException ex) {
            getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return list;
    }


    private void deleteBeautySkillRefineLog(long userId) {
        String sql = "delete from t_l_beauty_skill_refine where userId = ? and time < ?";
        ResultSet rs = null;
        CallableStatement call = null;
        long lastTime = System.currentTimeMillis() - 1000L * 100 * 3600 * 24;
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, userId);
            call.setTimestamp(i++, new Timestamp(lastTime));
            int rowNum = call.executeUpdate();
            if (rowNum > 0) {
                getLogger().error("user: {} delete beauty skill refine num: {}", userId, rowNum);
            }
        } catch (SQLException ex) {
            getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
    }


    private void deleteManorHarvestLog(long userId) {
        String sql = "delete from t_l_manor_harvest where userId = ? and time < ?";
        ResultSet rs = null;
        CallableStatement call = null;
        long lastTime = System.currentTimeMillis() - 1000L * 60 * 3600 * 24;
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, userId);
            call.setTimestamp(i++, new Timestamp(lastTime));
            int rowNum = call.executeUpdate();
            if (rowNum > 0) {
                getLogger().error("user: {} delete manor_harvest num: {}", userId, rowNum);
            }
        } catch (SQLException ex) {
            getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
    }


    private void deleteClinicLog(long userId) {
        String sql = "delete from t_l_clinic_cure where userId = ? and time < ?";
        ResultSet rs = null;
        CallableStatement call = null;
        long lastTime = System.currentTimeMillis() - 1000L * 100 * 3600 * 24;
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, userId);
            call.setTimestamp(i++, new Timestamp(lastTime));
            int rowNum = call.executeUpdate();
            if (rowNum > 0) {
                getLogger().error("user: {} delete t_l_clinic_cure num: {}", userId, rowNum);
            }
        } catch (SQLException ex) {
            getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
    }

    private void deleteBeautyGlamourLog(long userId) {
        String sql = "delete from t_l_beauty_glamour where userId = ? and time < ?";
        ResultSet rs = null;
        CallableStatement call = null;
        long lastTime = System.currentTimeMillis() - 1000L * 100 * 3600 * 24;
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, userId);
            call.setTimestamp(i++, new Timestamp(lastTime));
            int rowNum = call.executeUpdate();
            if (rowNum > 0) {
                getLogger().error("user: {} delete t_l_beauty_glamour num: {}", userId, rowNum);
            }
        } catch (SQLException ex) {
            getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
    }

    private void deletePatronsUpgradeLog(long userId) {
        String sql = "delete from t_l_patrons_upgrade where userId = ? and time < ?";
        ResultSet rs = null;
        CallableStatement call = null;
        long lastTime = System.currentTimeMillis() - 1000L * 100 * 3600 * 24;
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, userId);
            call.setTimestamp(i++, new Timestamp(lastTime));
            int rowNum = call.executeUpdate();
            if (rowNum > 0) {
                getLogger().error("user: {} delete t_l_patrons_upgrade num: {}", userId, rowNum);
            }
        } catch (SQLException ex) {
            getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
    }

    private void deleteBanquetRecord(long userId) {
        String sql = "delete from t_u_banquet_record where userId = ? and openTime < ?";
        ResultSet rs = null;
        CallableStatement call = null;
        long lastTime = System.currentTimeMillis() - 1000L * 60 * 3600 * 24;
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, userId);
            call.setLong(i++,lastTime);
            int rowNum = call.executeUpdate();
            if (rowNum > 0) {
                getLogger().error("user: {} delete t_u_banquet_record num: {}", userId, rowNum);
            }
        } catch (SQLException ex) {
            getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
    }


    private void deleteTradeWarRecord(long userId) {
        String sql = "delete from t_u_trade_war_record where userId = ? and recordTime < ?";
        ResultSet rs = null;
        CallableStatement call = null;
        long lastTime = System.currentTimeMillis()/1000 - 60 * 3600 * 24;
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, userId);
            call.setLong(i++,lastTime);
            int rowNum = call.executeUpdate();
            if (rowNum > 0) {
                getLogger().error("user: {} delete t_u_trade_war_record num: {}", userId, rowNum);
            }
        } catch (SQLException ex) {
            getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
    }













    private void deleteActivityData() {
        List<Integer> activityIdList = selectAllActivityId();
        for (Integer activityId : activityIdList) {
            Map<Integer, Long> endShowTimeMap = selectActivityEndShowTime(activityId);
            Long endShowTime = endShowTimeMap.get(activityId);
            if (endShowTime != null) {
                long now = System.currentTimeMillis() / 1000;
                if (now - endShowTime > 100 * 3600 * 24L) {
                   getLogger().error("expire activityId: {}", activityId);
                   deleteData(activityId);
                }
            }
        }
    }

    public List<Integer> selectAllActivityId() {
        String sql = "select DISTINCT activityId from t_u_activity_condition";
        List<Integer> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i = 1;
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(rs.getInt("activityId"));
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return list;
    }

    public Map<Integer, Long> selectActivityEndShowTime(int activityId) {
        Map<Integer, Long> activityEndShowTime = new ServerMgr().getActivityEndShowTime(activityId);
        return activityEndShowTime;
    }

    public void deleteData(int activityId) {
       /* String sql = "delete from t_u_activity_condition where activityId = " + activityId;
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i = 1;
            call.execute();
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }*/
    }
}
